package com.example.shopspring.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.shopspring.common.ResultData;
import com.example.shopspring.dto.StaSalesOutDto;
import com.example.shopspring.entity.StaSecond;
import com.example.shopspring.mapper.StaSecondMapper;
import com.example.shopspring.service.StaSecondService;
import com.example.shopspring.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class StaSecondServiceImpl extends ServiceImpl<StaSecondMapper, StaSecond> implements StaSecondService {

    @Override
    public ResultData getStaSales(String type) {
        log.info("业务：{}", type);
        //1:分类，hour，week,month
        //1.1 小时
        List<StaSecond> list = new ArrayList<>();
        if (type.equals("hour")) {
            LambdaQueryWrapper<StaSecond> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.ge(StaSecond::getRecordTime, DateUtils.twentyFourHoursAgo());
            list = list(queryWrapper);
        } else if (type.equals("week")) {
            list = baseMapper.getSalesRecordByDay(DateUtils.weekAgo());
        } else if (type.equals("month")) {
            list = baseMapper.getSalesRecordsByDay(DateUtils.monthAgo());
        } else {
            return ResultData.error(304, "参数错误");
        }
        StaSalesOutDto outDto = new StaSalesOutDto();
        if (list.size() > 0) {
            List<String> x = new ArrayList<>();
            List<String> y = new ArrayList<>();
            for (StaSecond staSecond : list) {
                x.add(DateUtils.dateToString(type, staSecond.getRecordTime()));
                y.add(staSecond.getRecordValue());
            }
            outDto.setX(x);
            outDto.setY(y);
        }
        return ResultData.success(outDto);
    }
}
